package com.hydl.power.websocket;


import com.hydl.power.mapper.SysUserMapper;
import com.hydl.power.pojo.SysUser;
import com.hydl.power.service.WebSocketService;
import com.hydl.power.service.impl.WebSocketServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.PostConstruct;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@ServerEndpoint("/webSocket/{sid}")
@Component
public class WebSocketServer {

    @Autowired
    private SysUserMapper sysUserMapper;
    private static SysUserMapper userMapper;
    @PostConstruct
    public void init(){
        userMapper = this.sysUserMapper;  //将注入的对象交给静态对象管理
    }


    private static WebSocketService webSocketService;

    @Autowired
    public void setSysUserMapper(WebSocketService webSocketService){
        WebSocketServer.webSocketService=webSocketService;
    }

    //静态变量，用来记录当前在线连接数。应该把它设计成线程安全的。
    private static AtomicInteger onlineNum = new AtomicInteger();

    //concurrent包的线程安全Set，用来存放每个客户端对应的WebSocketServer对象。
    public static ConcurrentHashMap<String, Session> sessionPools = new ConcurrentHashMap<>();

    //发送消息
    public void sendMessage(Session session, String message) throws IOException {
        if(session != null){
            synchronized (session) {
//                System.out.println("发送数据：" + message);
                session.getBasicRemote().sendText(message);
            }
        }
    }
    //给指定用户发送信息
    public void sendInfo(String userName, String message){
        Session session = sessionPools.get(userName);
        try {
            sendMessage(session, message);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //建立连接成功调用
    @OnOpen
    public void onOpen(Session session, @PathParam(value = "sid") String userName){
        sessionPools.put(userName, session);
        addOnlineCount();

    }

    //关闭连接时调用
    @OnClose
    public void onClose(@PathParam(value = "sid") String userName,Session session){
        String username = WebSocketServiceImpl.locationUserMap.get(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.locationMap.remove(username);
        WebSocketServiceImpl.locationUserMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.powerDataMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.powerImgMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.powerImgTemperatureDeviceMap.remove(session.getRequestParameterMap().get("sid").get(0));
       /* WebSocketServiceImpl.temperatureData.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.temperatureMap.remove(session.getRequestParameterMap().get("sid").get(0));*/
        WebSocketServiceImpl.customerMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.temperatureDataMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.temperatureDeviceMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.waterInvasion.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.voltage.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.tempDataMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.protectDataMap.remove(session.getRequestParameterMap().get("sid").get(0));
        sessionPools.remove(userName);
        subOnlineCount();
    }

    //收到客户端信息
    @OnMessage
    public void onMessage(String message,Session session) {
        if(message.contains("getPowerData")){
            //发送实时数据
            webSocketService.senPowerData(message,session);
        }else if(message.contains("getLocation")){
            webSocketService.senCarLocation(message,session);
        }else if(message.contains("getPowerImg")){
            webSocketService.senPowerImg(message,session);
        }/*else if(message.contains("getTemperature")){
            webSocketService.sendTemperature(message,session);
        }else if(message.contains("getPcTemperatureData")){
            webSocketService.sendTemperatureTemperaturePc(message,session);
        }*/else if(message.contains("getProjectAlarmStatus")){
            webSocketService.sendProjectAlarmStatus(message,session);
        }else if(message.contains("maintainerLocation")){
            //运维人员的位置
            webSocketService.addMaintainerLocation(message,session);
        }else if(message.contains("getMaintainerLocation")){
            //客户查看运维人员位置
            webSocketService.getMaintainerLocation(message,session);
        }else if (message.contains("getTemperatureData")){
            //线缆测温实时数据
            webSocketService.sendTemperatureData(message,session);
        }else if(message.contains("getTemperatureDeviceList")){
            //获取线缆测温列表
            webSocketService.getTemperatureDeviceList(message,session);
        }else if(message.contains("getWaterInvasion")){
            //水浸设备数据
            webSocketService.getWaterInvasion(message,session);
        }else if(message.contains("getVoltage")){
            //电压设备数据
            webSocketService.getVoltage(message,session);
        }else if (message.contains("getTempData")){
            //变压器温控数据
            webSocketService.getTempData(message,session);
        }else if (message.contains("getProtectData")){
            //综保数据
            webSocketService.getProtectData(message,session);
        }
        /* else{
            webSocketService.sendLocation(message);
        }*/

    }

    //错误时调用
    @OnError
    public void onError(Session session, Throwable throwable){
        String username = WebSocketServiceImpl.locationUserMap.get(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.locationMap.remove(username);
        WebSocketServiceImpl.locationUserMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.powerDataMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.powerImgMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.powerImgTemperatureDeviceMap.remove(session.getRequestParameterMap().get("sid").get(0));
        /*WebSocketServiceImpl.temperatureData.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.temperatureMap.remove(session.getRequestParameterMap().get("sid").get(0));*/
        WebSocketServiceImpl.temperatureDataMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.customerMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.temperatureDeviceMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.waterInvasion.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.voltage.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.tempDataMap.remove(session.getRequestParameterMap().get("sid").get(0));
        WebSocketServiceImpl.protectDataMap.remove(session.getRequestParameterMap().get("sid").get(0));
        System.out.println("发生错误");
        throwable.printStackTrace();
    }

    public static void addOnlineCount(){
        onlineNum.incrementAndGet();
    }

    public static void subOnlineCount() {
        onlineNum.decrementAndGet();
    }

}
